package com.hskj.rabbitmq.topicproducer;

import com.hskj.rabbitmq.util.RabbitMQUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class TopicProducer {
    public static final String QUEUE_NAME_ONE = "topic_queue1";
    public static final String QUEUE_NAME_TWO = "topic_queue2";
    public static final String EXCHANGE_NAME = "topic_exchange";

    public static void main(String[] args) throws Exception {
//        //创建连接工厂
//        ConnectionFactory connectionFactory = new ConnectionFactory();
//        //连接的设置
//        connectionFactory.setHost("192.168.6.144");
//        connectionFactory.setPort(5672);
//        connectionFactory.setVirtualHost("/");
//        connectionFactory.setUsername("admin");
//        connectionFactory.setPassword("admin");
//        //创建连接
//        Connection connection = connectionFactory.newConnection();
        Connection connection = RabbitMQUtil.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        // 声明交换机
        /**
         * @param exchange 交换机名称
         * @param type 交换机类型 DIRECT("direct"), FANOUT("fanout"), TOPIC("topic"), HEADERS("headers");
         * @param durable 指定交换机是否是持久的
         * @param autoDelete 指定交换机是否是自动删除的
         * @param internal 指定交换机是否是内部使用
         * @param arguments 其他参数
         */
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC,true,false,false,null);
        // 声明队列 两个队列
        /**
         * queue      参数1：队列名称
         * durable    参数2：是否定义持久化队列,当mq重启之后,还在
         * exclusive  参数3：是否独占本次连接
         *            ① 是否独占,只能有一个消费者监听这个队列
         *            ② 当connection关闭时,是否删除队列
         * autoDelete 参数4：是否在不使用的时候自动删除队列,当没有consumer时,自动删除
         * arguments  参数5：队列其它参数
         */
        channel.queueDeclare(QUEUE_NAME_ONE, true, false, false, null);
        channel.queueDeclare(QUEUE_NAME_TWO, true, false, false, null);

        // 交换机与队列进行绑定
        channel.queueBind(QUEUE_NAME_ONE,EXCHANGE_NAME,"#.fxp.#");
        channel.queueBind(QUEUE_NAME_ONE,EXCHANGE_NAME,"itfxp.#");
        channel.queueBind(QUEUE_NAME_TWO,EXCHANGE_NAME,"www.itfxp.*");
        channel.queueBind(QUEUE_NAME_TWO,EXCHANGE_NAME,"atitfxp.*");

        // 设置发送的消息
        String message1 = "该条消息发送到#.fxp.#匹配的队列中";
        // 发送消息
        channel.basicPublish(EXCHANGE_NAME, "a.fxp.c", null, message1.getBytes());

        // 设置发送的消息
        String message2 = "该条消息发送到itfxp.#匹配的队列中";
        // 发送消息
        channel.basicPublish(EXCHANGE_NAME, "itfxp.com", null, message2.getBytes());

        // 设置发送的消息
        String message3 = "该条消息发送到www.itfxp.*匹配的队列中";
        // 发送消息
        channel.basicPublish(EXCHANGE_NAME, "www.itfxp.com", null, message3.getBytes());

        // 设置发送的消息
        String message4 = "该条消息发送到atitfxp.*匹配的队列中";
        // 发送消息
        channel.basicPublish(EXCHANGE_NAME, "atitfxp.com", null, message4.getBytes());

        //关闭资源
        channel.close();
        connection.close();
    }
}
